<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class SalesPaymentLog extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];$map = [];
            $code = $this->request->post('purchase_code');
            if(!empty($code)){
                $where['i.bill_code'] = ['like','%'.$code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['i.bill_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $customers_id = $this->request->post('customers_id');
            if(!empty($customers_id)){
                $where['i.customers_id'] = $customers_id;
            }

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;
            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }

            if($pageStatus){
                $list = Db::name('kuyou_sales_payment_log')->alias('i')
                    ->join('kuyou_customers s','s.id=i.customers_id')
                    ->where($where)
                    ->page($page,$limit)
                    ->field('i.id,FROM_UNIXTIME(i.bill_time,\'%Y-%m-%d\') as bill_time,i.customers_id,i.sale_user,i.business_type,i.bill_code,
                i.bill_codes,i.purchase_money,i.money_tax_price,i.discount_money,i.real_money,i.customers_money,i.pay_money,i.give_money,i.reamrk,
                s.customer_name as s_name')
                    ->select();
            }else{
                $list = Db::name('kuyou_sales_payment_log')->alias('i')
                    ->join('kuyou_customers s','s.id=i.customers_id')
                    ->where($where)
                    ->field('i.id,FROM_UNIXTIME(i.bill_time,\'%Y-%m-%d\') as bill_time,i.customers_id,i.sale_user,i.business_type,i.bill_code,
                i.bill_codes,i.purchase_money,i.money_tax_price,i.discount_money,i.real_money,i.customers_money,i.pay_money,i.give_money,i.reamrk,
                s.customer_name as s_name')
                    ->select();
            }
            $row_count = Db::name('kuyou_sales_payment_log')->alias('i')
                ->join('kuyou_customers s','s.id=i.customers_id')
                ->where($where)->count();

            foreach ($list as $list_k=>$list_v){
                $list[$list_k]['sale_user'] = Db::name('kuyou_staff_member')->where(['id'=>$list_v['sale_user']])->value('name');
                $list[$list_k]['s_type'] = 1;
            }

            $price_decimal = \think\Db::name('kuyou_sets')->where(['name'=>'price_decimal'])->value('value');

            $oldcount = $count = [
                'money_tax_price'=>0,
                'purchase_money'=>0,
                'discount_money'=>0,
                'real_money'=>0,
                'customers_money'=>0,
                'pay_money'=>0,
                'give_money'=>0,
            ];
            $counts = $count;
            $counts['s_name'] = '合计';
            $counts['s_type'] = 3;
            $data = [];
            $field = 'bill_code';
            $now = isset($list[0][$field])?$list[0][$field]:'';
            $give_money = 0;
            $pay_money = 0;
            $status = true;
            if(!empty($list)){
                foreach ($list as $list_k=>$list_v){
                    $list[$list_k]['customer_name'] = $list_v['s_name'];
                    $list[$list_k]['sale_user'] = $list_v['sale_user'];
                    $list[$list_k]['payment_rate'] = '';

                    if($list_v[$field]!=$now){
                        $now = $list_v[$field];
                        $row = $count;
                        $row['s_name'] = '小计';$row['s_type'] = 2;$row['customer_name'] = '小计';$row['bill_time'] = '';$row['sale_user'] = '';
                        if(($count['real_money'])>0){
                            $row['payment_rate'] = round($count['pay_money']/($count['real_money']),4)*100 .'%';
                        }else{
                            $row['payment_rate'] = '';
                        }
                        $data[] = $row;
                        foreach ($count as $count_k=>$count_v){
                            $counts[$count_k] += $row[$count_k];
                        }
                        $status = true;
                    }

                    if($list_v[$field]==$now){
                        if($status){
                            $give_money = $list_v['give_money'];$pay_money = $list_v['pay_money'];
                            $status = false;
                            $count = $oldcount;
                        }else{
                            $pay_money += $list_v['pay_money'];
                            $give_money += $list_v['give_money'];
                        }
                        $list_v['give_money'] = $give_money;
                        if(!$status){
                            foreach ($count as $count_k=>$count_v){
                                $count[$count_k] += $list_v[$count_k];
                            }
                            $count['give_money'] = $list_v['give_money'];
                        }
                    }
                    $data[] = $list_v;
                    if($list_k==(sizeof($list)-1) || sizeof($list)==1){
                        $row = $count;$row['s_name'] = '小计';$row['s_type'] = 2;$row['customer_name'] = '小计';$row['bill_time'] = '';$row['sale_user'] = '';
                        if($row['real_money']>0){
                            $row['payment_rate'] = round($count['pay_money']/($count['real_money']),4)*100 .'%';
                        }else{
                            $row['payment_rate'] = '';
                        }
                        $data[] = $row;
                        foreach ($count as $count_k=>$count_v){
                            $counts[$count_k] += $row[$count_k];
                        }
                    }
                }

                if ($counts['real_money']>0){
                    $counts['payment_rate'] = round($counts['pay_money']/($counts['real_money']),4)*100 .'%';
                }else{
                    $counts['payment_rate'] = '';
                }
                $counts['customer_name'] = '合计';$counts['bill_time'] = '';$counts['sale_user'] = '';
                $counts['purchase_money'] = round($counts['purchase_money'],$price_decimal);
                $counts['discount_money'] = round($counts['discount_money'],$price_decimal);
                $counts['real_money'] = round($counts['real_money'],$price_decimal);
                $counts['customers_money'] = round($counts['customers_money'],$price_decimal);
                $counts['pay_money'] = round($counts['pay_money'],$price_decimal);
                $counts['give_money'] = round($counts['give_money'],$price_decimal);

                $data[] = $counts;
            }

            $result = array("count" => $row_count, "list" => $data);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
